package com.yz.group5.med_booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yz.group5.med_booking.entity.Doctor;
import com.yz.group5.med_booking.mapper.DoctorMapper;
import com.yz.group5.med_booking.service.DoctorService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {

    @Override
    public Page<Doctor> listDoctors(Map<String, Object> params) {
        // 分页参数
        long current = Long.parseLong(params.getOrDefault("page", 1).toString());
        long size = Long.parseLong(params.getOrDefault("pageSize", 10).toString());
        Page<Doctor> page = new Page<>(current, size);

        // 查询条件构建
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();

        // 医生姓名模糊查询（假设name字段在Doctor实体中存在）
        if (params.containsKey("name") && !StringUtils.isEmpty(params.get("name"))) {
            queryWrapper.like("name", params.get("name"));
        }

        // 科室ID精确查询 - 修改为deptId
        if (params.containsKey("departmentId") && params.get("departmentId") != null) {
            queryWrapper.eq("dept_id", params.get("departmentId"));
        }

        // 职称模糊查询
        if (params.containsKey("title") && !StringUtils.isEmpty(params.get("title"))) {
            queryWrapper.like("title", params.get("title"));
        }

        // 排序（默认按创建时间降序）
        queryWrapper.orderByDesc("created_at");

        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Doctor getDoctorById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean saveDoctor(Doctor doctor) {
        return save(doctor);
    }

    @Override
    public boolean updateDoctor(Doctor doctor) {
        return updateById(doctor);
    }

    @Override
    public boolean deleteDoctor(Integer id) {
        return removeById(id);
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        throw new UnsupportedOperationException("不支持状态更新操作");
    }
}